# coding=utf-8

import socket
import time
import threading
import logging
import sys
import os
logging.basicConfig(level=logging.INFO,
                format='%(asctime)s line:%(lineno)d %(levelname)s %(message)s',
                datefmt='%H:%M:%S',
                )


def thread(func, *args):
    t = threading.Thread(target=func, args=args)
    t.daemon = True
    t.start()



class CmdServer():
    def __init__(self):
        self.server_port = 44444

    def main(self):
        self.listen()


    def listen(self):
        proxy_server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        proxy_server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        proxy_server.bind(("0.0.0.0", self.server_port))
        proxy_server.listen(30)
        logging.info(("0.0.0.0", self.server_port))
        while True:
            client_conn, addr = proxy_server.accept()
            logging.info("received connect from %s:%s" %( addr[0], addr[1]))
            thread(self.recv, client_conn)
            while True:
                cmd = input()
                if not cmd:
                    cmd = " "
                client_conn.send(cmd.encode())
                

    def recv(self, client_conn):
        while True:
            data = client_conn.recv(1024)
            if data:
                sys.stdout.write(data.decode())
            time.sleep(0.01)


if __name__ == '__main__':

    CmdServer().main()
    while 1:
        time.sleep(10000)

        
        